package cn.ucox.web.ms.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Properties;

/**
 * SpringData配置
 *
 * @author chenw
 * @create 2016-12-29 12:05
 * @email javacspring@gmail.com
 */
@Configuration
@Import(DataExchangeConfig.class)
@MapperScan(basePackages = {"cn.ucox.web.ms.persistence"})
@EnableTransactionManagement
class MyBatisConfig implements TransactionManagementConfigurer {

    public static String BASE_PACKAGE = "cn.ucox.web.ms";

    @Resource(name = "pollingDataSource")
    DataSource dataSource;

    @Resource
    DataExchangeConfig.PollingConfig jdbcConfig;

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory() {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
//        bean.setTypeAliasesPackage(BASE_PACKAGE_DOMAIN_LAYER);
//        bean.setTypeAliases(new Class[]{Query.class});

        PageInterceptor pageInterceptor = new PageInterceptor();
        pageInterceptor.setProperties(new Properties() {{
            put("reasonable", "true");
            put("supportMethodsArguments", "true");
            put("returnPageInfo", "check");
        }});
        bean.setPlugins(new Interceptor[]{pageInterceptor});
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            String mybatisMapperPath = "classpath:".concat(BASE_PACKAGE.replaceAll("\\.", "/")).concat("/persistence/").concat(jdbcConfig.getDbType()).concat("/*.xml");
            bean.setMapperLocations(resolver.getResources(mybatisMapperPath));
            return bean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return new DataSourceTransactionManager(dataSource);
    }

}
